package com.xiaojun.whut.Service.Impl;

import com.xiaojun.whut.Entity.Role;
import com.xiaojun.whut.Entity.User;
import com.xiaojun.whut.Repository.RoleRepository;
import com.xiaojun.whut.Service.UserService;
import com.xiaojun.whut.Repository.UserRepository;
import com.xiaojun.whut.util.JacksonUtil;
import com.xiaojun.whut.util.ResponseCode;
import com.xiaojun.whut.util.ResponseString;
import com.xiaojun.whut.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ResponseStatusException;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserRepository userRepository;
    @Autowired
    private RoleRepository roleRepository;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public Object register(User newUser) {
        System.out.println(newUser.toString());
        if(newUser==null){
            return ResponseUtil.badArgument();
        }
        boolean phoneNumberFinded=userRepository.existsByPhoneNumber(newUser.getPhoneNumber());
        boolean isUserNameFinded=userRepository.existsByUserName(newUser.getUsername());
        boolean isEmailFinded=userRepository.existsByEmail(newUser.getEmail());
        Map<Object,Object> result=new HashMap<>();
        if(isUserNameFinded){
            return ResponseUtil.fail(ResponseCode.USER_NAME_ALRREAY_EXIST_CODE,
                    ResponseString.USER_NAME_ALRREAY_EXIST);
        }
        if(phoneNumberFinded){
            return ResponseUtil.fail(ResponseCode.USER_PHONE_NUMBER_ALRREAY_EXIST_CODE,
                    ResponseString.USER_PHONE_NUMBER_ALRREAY_EXIST);
        }
        if(isEmailFinded){
            return ResponseUtil.fail(ResponseCode.USER_EMAIL_ALRREAY_EXIST_CODE,
                    ResponseString.USER_EMAIL_ALRREAY_EXIST);
        }
        try {
            Role role=roleRepository.findByName("user");
            newUser.setPassword(passwordEncoder.encode(newUser.getPassword()));
            newUser.getRoles().add(role);
            userRepository.save(newUser);
            result.put("userName",newUser.getUsername());
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return ResponseUtil.ok(result);

    }

    @Override
    public Object login(String body) {
        System.out.println(body);
        String userName = JacksonUtil.parseString(body, "userName");
        String password = JacksonUtil.parseString(body, "password");
        if(userName==null || password==null){
            return ResponseUtil.badArgument();
        }
        User user=userRepository.findByUserName(userName);
        Role role=roleRepository.findByName("user");
        if(user==null){
            return ResponseUtil.fail(ResponseCode.USER_NOT_EXIST_CODE,ResponseString.USER_NOT_EXIST);
        }
        if(!user.getRoles().contains(role)){
            return ResponseUtil.fail(ResponseCode.NOT_AUTHORITY_VISIT_CODE,ResponseString.NOT_AUTHORITY_VISIT);
        }
        if(!passwordEncoder.matches(password,user.getPassword())){
            return ResponseUtil.fail(ResponseCode.AUTH_INVALID_ACCOUNT_CODE,ResponseString.AUTH_INVALID_ACCOUNT);
        }
        Map<Object,Object> result=new HashMap<>();
        result.put("userName",userName);
        result.put("phoneNumber",user.getPhoneNumber());
        result.put("collage",user.getCollage());
        result.put("email",user.getEmail());
        result.put("sex",user.getSex());
        return ResponseUtil.ok(result);
    }
}
